﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class tapNote : MonoBehaviour
{
    public float noteSpeed;
    public KeyCode keyToPress;
    public bool keyPressed = false;
    public GameObject pureEffect;
    public GameObject farEffect;
    public GameObject lostEffect;
    public GameObject hitPureEffect;
    public GameObject hitFarEffect;
    public GameObject hitLostEffect;
    public GameObject k1, k2, k3, k4; //传入4个键位 为了获取其位置赋给hit效果
    private GameObject hitEffect;//保存实例化后的hit效果
    private GameObject hitEffect2;//保存实例化后的打击特效
    Vector3 effectPos; //hit效果位置
    float noteScale; //note适应分辨率缩放比例
    // Start is called before the first frame update
    void Start()
    {
        noteScale = (GlobalSettings.Instance.standardWidth / GlobalSettings.Instance.standardHeight) / ((float)Screen.width / (float)Screen.height);
        transform.localScale = transform.localScale / noteScale;
    }

    // Update is called once per frame
    void Update()
    {
        transform.position -= new Vector3(0f, noteSpeed * Time.deltaTime, 0f);
        if (transform.position.y < GlobalSettings.Instance.k1Pos.y + 0.12f * noteSpeed)
        {
            if (keyToPress == GlobalSettings.Instance.key1)
            {
                GlobalSettings.Instance.k1InZone = true;
                effectPos = k1.GetComponent<RectTransform>().anchoredPosition;
                if (GlobalSettings.Instance.k1Pressed == true)
                {
                    keyPressed = true;
                    GlobalSettings.Instance.k1Pressed = false;
                    GlobalSettings.Instance.k1InZone = false;
                }
                else
                {
                    GlobalSettings.Instance.k1InZone = missLost();
                }
            }
            else if (keyToPress == GlobalSettings.Instance.key2)
            {
                GlobalSettings.Instance.k2InZone = true;
                effectPos = k2.GetComponent<RectTransform>().anchoredPosition;
                if (GlobalSettings.Instance.k2Pressed == true)
                {
                    keyPressed = true;
                    GlobalSettings.Instance.k2Pressed = false;
                    GlobalSettings.Instance.k2InZone = false;
                }
                else
                {
                    GlobalSettings.Instance.k2InZone = missLost();
                }
            }
            else if (keyToPress == GlobalSettings.Instance.key3)
            {
                GlobalSettings.Instance.k3InZone = true;
                effectPos = k3.GetComponent<RectTransform>().anchoredPosition;
                if (GlobalSettings.Instance.k3Pressed == true)
                {
                    keyPressed = true;
                    GlobalSettings.Instance.k3Pressed = false;
                    GlobalSettings.Instance.k3InZone = false;
                }
                else
                {
                    GlobalSettings.Instance.k3InZone = missLost();
                }
            }
            else if (keyToPress == GlobalSettings.Instance.key4)
            {
                GlobalSettings.Instance.k4InZone = true;
                effectPos = k4.GetComponent<RectTransform>().anchoredPosition;
                if (GlobalSettings.Instance.k4Pressed == true)
                {
                    keyPressed = true;
                    GlobalSettings.Instance.k4Pressed = false;
                    GlobalSettings.Instance.k4InZone = false;
                }
                else
                {
                    GlobalSettings.Instance.k4InZone = missLost();
                }
            }
            if (keyPressed == true)
            {
                if (Mathf.Abs(transform.position.y - GlobalSettings.Instance.k1Pos.y) > 0.1f * noteSpeed)
                {
                    GameManager.Instance.lostHit();
                    keyPressed = false;
                    hitEffect = Instantiate(lostEffect, GameObject.Find("EffectCanvas").GetComponent<Transform>());
                    hitEffect.GetComponent<RectTransform>().anchoredPosition = effectPos + new Vector3(0f,80f,0f);
                    hitEffect2 = Instantiate(hitLostEffect);
                    hitEffect2.transform.position = getKeyPos();
                    notePool.Instance.RecycleObj(this.gameObject);
                }
                else if (Mathf.Abs(transform.position.y - GlobalSettings.Instance.k1Pos.y) > 0.05f * noteSpeed)
                {
                    GameManager.Instance.farHit();
                    keyPressed = false;
                    hitEffect = Instantiate(farEffect, GameObject.Find("EffectCanvas").GetComponent<Transform>());
                    hitEffect.GetComponent<RectTransform>().anchoredPosition = effectPos + new Vector3(0f, 80f, 0f);
                    hitEffect2 = Instantiate(hitFarEffect);
                    hitEffect2.transform.position = getKeyPos();
                    notePool.Instance.RecycleObj(this.gameObject);                  
                }
                else
                {
                    GameManager.Instance.pureHit();
                    keyPressed = false;
                    hitEffect = Instantiate(pureEffect, GameObject.Find("EffectCanvas").GetComponent<Transform>());
                    hitEffect.GetComponent<RectTransform>().anchoredPosition = effectPos + new Vector3(0f, 80f, 0f);
                    hitEffect2 = Instantiate(hitPureEffect);
                    hitEffect2.transform.position = getKeyPos();
                    notePool.Instance.RecycleObj(this.gameObject);
                }
            }
        }
    }

    private bool missLost() //将漏掉导致的lost的判定以及回收音符写进函数里 方便赋值给对应的keyinzone
    {
        if (transform.position.y < GlobalSettings.Instance.k1Pos.y - 0.1f * noteSpeed)
        {
            GameManager.Instance.lostHit();
            hitEffect = Instantiate(lostEffect, GameObject.Find("EffectCanvas").GetComponent<Transform>());
            hitEffect.GetComponent<RectTransform>().anchoredPosition = effectPos + new Vector3(0f, 80f, 0f);
            hitEffect2 = Instantiate(hitLostEffect);
            hitEffect2.transform.position = getKeyPos();
            notePool.Instance.RecycleObj(this.gameObject);
            return false;
        }
        else
        {
            return true;
        }
    }

    private Vector3 getKeyPos()
    {
        if (keyToPress == GlobalSettings.Instance.key1)
        {
            return GlobalSettings.Instance.k1Pos;
        }
        else if (keyToPress == GlobalSettings.Instance.key2)
        {
            return GlobalSettings.Instance.k2Pos;
        }
        else if (keyToPress == GlobalSettings.Instance.key3)
        {
            return GlobalSettings.Instance.k3Pos;
        }
        else
        {
            return GlobalSettings.Instance.k4Pos;
        }
    }
}
    




